/*
 * jopengl_Font.cpp
 *
 *  Created on: 2013-10-31
 *      Author: pan.jinghua
 */

#include <windows.h>
#include <stdio.h>

#include "jopengl_Font.h"
#include "util.h"

#include <ft2build.h>
#include <freetype/freetype.h>
#include <freetype/ftglyph.h>
#include <freetype/ftoutln.h>
#include <freetype/fttrigon.h>

jlong JNICALL Java_jopengl_Font__1loadLibrary(JNIEnv *env, jclass clz) {
    FT_Library library;
    if (FT_Init_FreeType(&library)) {
        library = 0;
    }
    return (jlong)library;
}

jlong JNICALL Java_jopengl_Font__1loadFace(JNIEnv *env, jclass clz, jlong libraryAddr, jstring file) {
    FT_Library library = (FT_Library)libraryAddr;
    FT_Face face;
    const char* fileUtf8 = env->GetStringUTFChars(file, 0);
    const char* fileGBK = conv(fileUtf8, CP_UTF8, CP_ACP);
    if (FT_New_Face(library, fileGBK, 0, &face)) {
        face = 0;
    } else {
        FT_Select_Charmap(face, FT_ENCODING_UNICODE);
    }
    env->ReleaseStringUTFChars(file, fileUtf8);
    delete[] fileGBK;
    return (jlong)face;
}

void JNICALL Java_jopengl_Font__1freeFace(JNIEnv *env, jclass clz, jlong faceAddr) {
    FT_Done_Face((FT_Face)faceAddr);
}

void JNICALL Java_jopengl_Font__1setHeight(JNIEnv *env, jclass clz, jlong faceAddr, jint height) {
    FT_Set_Pixel_Sizes((FT_Face)faceAddr, 0, height);
}

